﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace PIM_ATE
{
    public partial class FrmConfigNewAntenna : Form
    {
        public FrmConfigNewAntenna(ClsDataConfigLocalBuilder cfgBuilder)
        {
            InitializeComponent();
            m_cfgBuilder = cfgBuilder;
        }

        ClsDataConfigLocalBuilder m_cfgBuilder;
        List<ClsDataConfigLocal> m_lstConfig;
        DataTable m_dtConfig;

        private void FrmNewConfigAntenna_Load(object sender, EventArgs e)
        {
            dvgConfig.RowHeadersVisible = false;
            dvgConfig.AllowUserToAddRows = false;
            dvgConfig.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dvgConfig.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            dvgConfig.MultiSelect = false;

            cbTestType.SelectedIndex = 0;
            cbBand.SelectedIndex = 0;
            checkManualRCU.Checked = false;
            m_lstConfig = new List<ClsDataConfigLocal>();
            cbretspeed.SelectedIndex = 2;

            if (!GDefine.IS_LOCAL_CONFIG_ANTENNA)
            {
                var resultObj = new ClsDataResultForAntennaBuilder(GDefine.DBNamePIM);
                string[] lstName = resultObj.getProcodeListFromTMO();
                if (lstName != null)
                {
                    cbProcode.Items.AddRange(lstName);
                }
                cbProcode.Visible = true;
                textProcode.Visible = false;
                btnSaveLocal.Visible = false;
            }
            else
            {
                cbProcode.Visible = false;
                textProcode.Visible = true;
                btnSaveLocal.Visible = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!GDefine.IS_LOCAL_CONFIG_ANTENNA && cbProcode.Text == "")
            {
                MessageBox.Show("请输入!");
                return;
            }

            textRcuIndex.Text = textRcuIndex.Text.Replace(" ", "");
            if (textAngle.Text == "" || textRcuIndex.Text == "")
            {
                MessageBox.Show("不允许空和空格，请重新输入!");
                return;
            }

            string[] sRCUIndex = textRcuIndex.Text.Split(',');
            if (sRCUIndex != null && sRCUIndex.Length >= 2)
            {
                if (sRCUIndex[0] == sRCUIndex[1])
                {
                    MessageBox.Show("RCU端口重复！");
                    return;
                }
            }
            List<string> lstRCUIndex = new List<string>();
            List<string> lstPortTip = new List<string>();
            foreach (var str in sRCUIndex)
            {
                string[] strs = str.Split('-');
                if (strs.Length == 2)
                {
                    //lstRCUIndex.Add(str + "+45度");
                    //lstRCUIndex.Add(str + "-45度");

                    //new
                    lstRCUIndex.Add(strs[0] + "+45度");
                    lstRCUIndex.Add(strs[0] + "-45度");
                    lstPortTip.Add(strs[1]);
                    lstPortTip.Add(strs[1]);

                }
                else
                {
                    lstRCUIndex.Add(strs[0] + "+45度");
                    lstRCUIndex.Add(strs[0] + "-45度");
                    lstPortTip.Add(strs[0]);
                    lstPortTip.Add(strs[0]);
                }
            }
            string[] sAngle = textAngle.Text.Split(',');

            if (!checkManualRCU.Checked && sAngle.Length != 2)
            {
                MessageBox.Show("自动调度只能设置2个角度(最大和最小)");
                return;
            }          

            foreach (var str in sAngle)
            {
                try { Convert.ToInt32(str); }
                catch { MessageBox.Show("角度输入错误！"); return; }
            }

            int time = 10;
            Regex rx = new Regex(@"(\d+)\.(\d+)|(\d+)");
            MatchCollection ms = rx.Matches(txtTime.Text.Trim());
            if (ms.Count == 1)
            {
                time =Convert.ToInt32(ms[0].Value);             
            }
            else
            {
                MessageBox.Show("点频时间输入错误！"); return;
            }

            ClsDataConfigLocal[] lstConfig = new ClsDataConfigLocal[lstRCUIndex.Count * sAngle.Length];
            for (int n = 0, j = 0; j < sAngle.Length; j++)
            {           
                 for (int i = 0; i < lstRCUIndex.Count; i++)
                {
                    lstConfig[n] = new ClsDataConfigLocal();
                    lstConfig[n].ID = 0;
                    lstConfig[n].Enable = true;
                    lstConfig[n].ProCode = cbProcode.Text;
                    lstConfig[n].RetSpeed = cbretspeed.SelectedIndex;
                    lstConfig[n].RCUIndex = lstRCUIndex[i];
                    lstConfig[n].TestAngle = Convert.ToInt32(sAngle[j]);
                    lstConfig[n].BandAlias = cbBand.SelectedItem.ToString();
                    lstConfig[n].Message = "";
                    lstConfig[n].SpecUnit = "dBm";
                    lstConfig[n].IsKnock = checkManualRCU.Checked;
                    lstConfig[n].PIMItemName = "天线" + 
                                               lstConfig[n].BandAlias + "/" + 
                                               lstConfig[n].RCUIndex + "/" + 
                                               lstConfig[n].TestAngle;

                    lstConfig[n].IsSweepTest = cbTestType.SelectedIndex == 0;
                    lstConfig[n].PIMOrder = 3;
                    lstConfig[n].SpecReg = nudSpec.Value.ToString();
                    lstConfig[n].TX1L = 0;
                    lstConfig[n].TX1H = 0;
                    lstConfig[n].TX2L = 0;
                    lstConfig[n].TX2H = 0;
                    lstConfig[n].RXL = 0;
                    lstConfig[n].RXH = 0;
                    lstConfig[n].TestStep1 = 0;
                    lstConfig[n].TestStep2 = 0;
                    lstConfig[n].F1Freq = 0;
                    lstConfig[n].F2Freq = 0;
                    lstConfig[n].PIMFreq = 0;
                    lstConfig[n].TestTime = time;
                    lstConfig[n].F1Power = Convert.ToDouble(nudTx.Value);
                    lstConfig[n].F2Power = Convert.ToDouble(nudTx.Value);
                    lstConfig[n].PortTip = lstPortTip[i];

                    lstConfig[n].FileVer = textFileVersion.Text.Trim();
                    n++;
                }
            }

            m_lstConfig.AddRange(lstConfig);

            //DataView dtView = ClsDataConfigLocalBuilder.toTable(m_lstConfig).DefaultView;
            //dtView.Sort = "TestAngle";
            //m_dtConfig = dtView.ToTable();
            //dvgConfig.DataSource = m_dtConfig;

            m_dtConfig = ClsDataConfigLocalBuilder.toTable(m_lstConfig);
            dvgConfig.DataSource = m_dtConfig;
            clearCtl();
        }

        void clearCtl()
        {
            textRcuIndex.Text = "";
            textAngle.Text = "";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_cfgBuilder == null)
            {
                return;
            }

            if (textVersion.Text == "" || cbProcode.Text == "")
            {
                MessageBox.Show("请输入!");
                return;
            }

            if (m_lstConfig.Count == 0)
            {
                MessageBox.Show("当前配置空!");
                return;
            }

            ClsDataConfigLocalBuilder cfgObj = new ClsDataConfigLocalBuilder(GDefine.DBNamePIM);
            if (cfgObj.constructByProcode(cbProcode.Text).Length != 0)
            {
                MessageBox.Show("该名称已配置");
                return;
            }

            string error = "";
            if (m_cfgBuilder.upload(cbProcode.Text, textVersion.Text, m_lstConfig, ref error))
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clearCtl();
            m_lstConfig.Clear();
            m_dtConfig.Clear();
        }

        private void btnSaveLocal_Click(object sender, EventArgs e)
        {
            if (textVersion.Text == "" || textProcode.Text == "")
            {
                MessageBox.Show("请输入!");
                return;
            }

            if (m_lstConfig.Count == 0)
            {
                MessageBox.Show("当前配置空!");
                return;
            }

            if (ClsDataConfigLocalBuilder.SerializeFile(Application.StartupPath + "\\config\\" + textProcode.Text + GDefine.SUFFIX_STRING_PIM, 
                                                        m_lstConfig.ToArray()))
            {
                MessageBox.Show("保存本地成功!");
            }
            else
            {
                MessageBox.Show("保存本地失败!");
            }
        }

    }
}
